<?php

namespace app\common\controller;

use app\BaseController;
use app\model\AuthRuleModel;
use app\model\AuthRuleTypeModel;
use think\App;

class Auth extends BaseController
{
    protected $mode;
    private $upload; // 上传类
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->mode = new AuthRuleModel();
        // 实例化上传类
        $upload = new \app\common\Upload();
        // 设置上传类型和文件大小
        $this->upload = $upload->local()->size(50);
    }

    /**
     * 权限规则类型
     * @return bool|string
     */
    public function typeList(): bool|string
    {
        try {
            $where = [];
            $where[] = ['status','=',1];
            $AuthRuleTypeModel = new AuthRuleTypeModel();
            $data = $AuthRuleTypeModel->field('id,name,img,sort')->where($where)->order('sort asc')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 权限规则类型-添加
     * @return bool|string
     */
    public function typeAdd(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'name|类型' => 'require',
                'status|状态'  => 'require',
            ]);
            $AuthRuleTypeModel = new AuthRuleTypeModel();
            $param['time'] = time();
            $AuthRuleTypeModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 权限规则类型-修改
     * @return bool|string
     */
    public function typeUpdate(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require|integer|max:11',
            ]);
            $AuthRuleTypeModel = new AuthRuleTypeModel();
            $info = $AuthRuleTypeModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 权限规则类型-删除
     * @return bool|string
     */
    public function typeDel(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $AuthRuleTypeModel = new AuthRuleTypeModel();
            if (is_array($id)){
                $AuthRuleTypeModel->where('id','in',$id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }else{
                $AuthRuleTypeModel->where('id','=',$id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 权限规则
     * @return false|string
     */
    public function authRule(): bool|string
    {
        $param = $this->request->param();
        $where = [];
        $field = 'id,pid,name,url,icon,icon_shortcut,icon_task,sort,induce,mode,status,type,c_type,type_id,is_must,is_must1,is_must2,is_must3';
        if (isset($param['mode']) && strlen($param['mode']) > 0) {
            $where[] = ['mode', 'like', '%'.$param['mode'].'%'];
            $where[] = ['status', '=', 1];
            if ($param['mode'] != 0){
                $field = 'id,pid,name,url,icon,icon_shortcut,icon_task,sort,induce,mode,status,type,is_must'.$param['mode'].' as is_must,c_type,type_id';
            }
        }
        $menu = $this->mode->getAllMenu($where,$field);
        return success($menu);
    }

    /**
     * 详情
     * @return bool|string
     */
    public function info(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $info = $this->mode->find($id);
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 添加权限规则
     * @return false|string
     */
    public function add(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, $this->mode->addValidate());
            $param['time'] = time();
            // 上传图标
            $param['icon'] = $this->iconFile('icon');
            // 上传快捷方式图标
            $param['icon_shortcut'] = $this->iconFile('icon_shortcut');
            // 上传任务图标
            $param['icon_task'] = $this->iconFile('icon_task');
            $this->mode->insert($param);
            return success([],'添加成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }

    }

    /**
     * 编辑权限规则
     * @return false|string
     */
    public function update(): bool|string
    {
        try {
            $param = $this->request->param();
            $info =  $this->mode->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            // 上传图标
            $icon = $this->iconFile('icon');
            if (!empty($icon)){
                $param['icon'] = $icon;
            }
            // 上传快捷方式图标
            $icon_shortcut = $this->iconFile('icon_shortcut');
            if (!empty($icon_shortcut)){
                $param['icon_shortcut'] = $icon_shortcut;
            }
            // 上传任务图标
            $icon_task = $this->iconFile('icon_task');
            if (!empty($icon_task)){
                $param['icon_task'] = $icon_task;
            }
            $info->save($param);
            return success([],'更新成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 删除权限规则
     * @return false|string
     */
    public function del(): bool|string
    {
        $id = $this->request->param('id');
        try {
            $this->mode->where('id','=',$id)->update([
                'is_del' => 1,
                'del_time' => time()
            ]);
            return success([],'删除成功！');
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 上传图标
     */
    public function iconFile($name)
    {
        $files = $this->request->file($name);
        if(empty($files)) return '';
        $upload = new \app\common\Upload();
        // 设置上传类型和文件大小
        $upload = $upload->defaultConfig();
        // 上传操作
        if(is_array($files)) {
            // 上传多文件
            $src = [];
            foreach ($files as $file) {
                $src[] = $upload->upload(file_get_contents($file),'binary','.png');
            }
        }else {
            // 上传单文件
            $src = $upload->upload(file_get_contents($files),'binary','.png');
        }
        return $src;
    }

}
